package com.naiterui.ehp.bs.recommend.util;

import cn.hutool.core.util.StrUtil;

/**
 * 用药工具类
 * 
 * @author yangfuya
 * @since 2021/11/30
 */
public class MedicationUtil {

    /**
     * 获取符合用药周期
     * 
     * @param dosageCycle
     *            用量周期
     * @param dosageCycleUnit
     *            用量周期单位
     * @param dosageCount
     *            用量次数
     * @return 是否符合
     */
    public static Boolean getAccordWithDrugCylce(double dosageCycle, String dosageCycleUnit, String dosageCount) {
        if (StrUtil.isBlank(dosageCycleUnit) || StrUtil.isBlank(dosageCount)) {
            return false;
        }
        try {
            int count = Integer.parseInt(dosageCount);
            // 用药周期在每天一次、两次、三次、四次，每1小时、每2小时、每3小时
            return (dosageCycle == 1 && ("天".equals(dosageCycleUnit) || "日".equals(dosageCycleUnit))
                && (count == 1 || count == 2 || count == 3 || count == 4))
                || ("小时".equals(dosageCycleUnit) && count == 1
                    && (dosageCycle == 1 || dosageCycle == 2 || dosageCycle == 3));
        } catch (NumberFormatException e) {
            return false;
        }
    }

    /**
     * 算出用药周期数
     *
     * @param dosageCycle
     *            用量周期
     * @param dosageCycleUnit
     *            用量周期单位
     * @param dosageCount
     *            用量次数
     * @return 是否符合
     */
    public static int getAssistDailyFrequency(double dosageCycle, String dosageCycleUnit, String dosageCount) {
        if (getAccordWithDrugCylce(dosageCycle, dosageCycleUnit, dosageCount)) {
            int count = Integer.parseInt(dosageCount);
            if ("天".equals(dosageCycleUnit)) {
                return count;
            } else {
                return (int)(24 / dosageCycle);
            }
        } else {
            return 0;
        }

    }
    /**
     * 获取每日的用药频次
     *
     * @param durgCycle
     *            用药周期
     * @param dosageCycle
     *            用量周期
     * @param dosageCycleUnit
     *            用量周期单位
     * @return 每日的用药频次
     */
    public static Double getDailyFrequency(double durgCycle, double dosageCycle, String dosageCycleUnit) {
        double unitCycle;
        switch (dosageCycleUnit) {
            case "小时":
                unitCycle = durgCycle * 24;
                break;
            case "周":
                unitCycle = durgCycle / 7;
                break;
            case "月":
                unitCycle = durgCycle / 30;
                break;
            default:
                unitCycle = durgCycle;
                break;
        }
        // 用药周期 / 用量周期（相当于多久吃一次） = 每次频次
        return unitCycle / dosageCycle;
    }
}
